package com.cloudptt.api.product.func.message;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.cloudptt.api.models.msg.MsgExtStrInfo;
import com.cloudptt.api.pjsip.api.SipManager;
import com.cloudptt.api.product.func.IBroadcast;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.MsgOperator;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.SessionInfo;
import com.mcptt.db.op.HistoryDbOperator;
import com.mcptt.db.op.MapDbOperator;

public class MsgBroadcast extends BroadcastReceiver implements IBroadcast
{
	private Context ctx = null;
	
	private NotifyListener notifyListener = null;
	private AppLogListener appLogListener = null;
	private MsgListener msgListener = null;
	private HistoryDbOperator dbOperater = null;
	private MapDbOperator onlineDbOperator = null;
//	private MsgConfig config = null;
//	private MsgOperator operator = null;
	private MsgOperator msgOp = null;


	public MsgBroadcast(Context ctx, HistoryDbOperator dbOperater, MapDbOperator onlineDbOperator,MsgOperator msgOp)
	{
		Log.d(MsgFunc.Log_Key, "MsgBroadcast");
		this.ctx = ctx;
		this.dbOperater = dbOperater;
		this.onlineDbOperator = onlineDbOperator;
		this.msgOp = msgOp;
//		this.config = config;
//		this.operator = operator;
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_CALL_LOG));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_MESSAGE));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_NOTIFY));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_APPLOG));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_ADDR_UPT));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_ONLINE));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_SOS));
	}

	public void setMsgListener(MsgListener listener) {
		this.msgListener = listener;
	}
	
	public void setNotifyListener(NotifyListener listener) {
		this.notifyListener = listener;
	}

	public void setAppLogListener(AppLogListener appLogListener) {
		Log.d(MsgFunc.Log_Key, "setAppLogListener setAppLogListener = " + appLogListener);
		this.appLogListener = appLogListener;
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		Log.d(MsgFunc.Log_Key, "Action is " + action);

		if(intent.getAction().equals(SipManager.ACTION_SIP_CALL_LOG)) //接受短消息广播
		{
			if(msgListener != null)
			{
				this.msgListener.receiveMsg();
			}
		}
		else if(intent.getAction().equals(SipManager.ACTION_SIP_MESSAGE)) //接受短消息广播
		{
			String fromStr = intent.getStringExtra(SipManager.FROML_STR);
			String infoStr = intent.getStringExtra(SipManager.Info_Str);
			String msgStr = intent.getStringExtra(SipManager.Body_Str);
			Log.d(MsgFunc.Log_Key, "ACTION_SIP_MESSAGE fromStr = " + fromStr + " infoStr = " + infoStr +  " msgStr = " + msgStr);
			
			MsgExtStrInfo extStrInfo = new MsgExtStrInfo(infoStr);
			int sessionType = extStrInfo.getSessionType();
			int msgType = extStrInfo.getMsgType();
//			long len = extStrInfo.getLen();
			String senderNum = extStrInfo.getValue(MsgExtStrInfo.Key_PNum);
			Log.d(MsgFunc.Log_Key, "ACTION_SIP_MESSAGE sessionType = " + sessionType + " senderNum = " + senderNum + " msgType = " + msgType);

//			int sessionType = intent.getIntExtra(SipManager.SESSION_TYPE_STR,SessionInfo.SessionType_Single);
//			int msgType = intent.getIntExtra(SipManager.MSG_TYPE_STR,SessionInfo.Msgtype_Txt);
//			String senderNum = intent.getStringExtra(SipManager.SENDER_STR);
//			Log.d(MsgFunc.Log_Key, "ACTION_SIP_MESSAGE sessionType = " + sessionType + " senderNum = " + senderNum);

			if(msgOp != null)
			{
				msgOp.notification();
			}

			AddrInfo receiver = null;
			AddrInfo sender = null;
			receiver = dbOperater.getUserInfoByNum(fromStr);
			if(receiver == null)
			{
				receiver  = dbOperater.getGroupInfoByNum(fromStr);
			}
			Log.d(MsgFunc.Log_Key, "ACTION_SIP_MESSAGE receiver = " + receiver);
			if(sessionType == SessionInfo.SessionType_Group)
			{
				sender = dbOperater.getUserInfoByNum(senderNum);
			}
			else
			{
				sender = receiver;

			}
			Log.d(MsgFunc.Log_Key, "ACTION_SIP_MESSAGE sender = " + sender);
			SessionInfo session  = new SessionInfo(receiver,sender,msgType,msgStr,0);
			this.dbOperater.insertNewSession(session,false);
			if(msgListener != null)
			{
				this.msgListener.receiveMsg();
			}
		}
		else if(intent.getAction().equals(SipManager.ACTION_SIP_NOTIFY)) //接受短消息广播
		{
			String msgStr = intent.getStringExtra(SipManager.CONTENT);
			Log.d(MsgFunc.Log_Key, "ACTION_SIP_MESSAGE msgStr = " + msgStr);
			if(notifyListener != null
					&& msgStr != null
					&& msgStr.length() > 0)
			{
				this.notifyListener.receiveNotify(msgStr);
			}
		}
		else if(intent.getAction().equals(SipManager.ACTION_SIP_SOS)) //接受短消息广播
		{
			String infoStr = intent.getStringExtra(SipManager.Info_Str);
			Log.d(MsgFunc.Log_Key, "ACTION_SIP_MESSAGE infoStr = " + infoStr);

			MsgExtStrInfo extStrInfo = new MsgExtStrInfo(infoStr);
			String num = extStrInfo.getValue(MsgExtStrInfo.Key_AlarmNum);
			Log.d(MsgFunc.Log_Key, "ACTION_SIP_SOS num = " + num);
			if(notifyListener != null)
			{
				this.notifyListener.receiveSos(num);
			}
		}
		else if(intent.getAction().equals(SipManager.ACTION_SIP_APPLOG)) //接受短消息广播
		{
			Log.d(MsgFunc.Log_Key, "ACTION_SIP_APPLOG appLogListener = " + appLogListener );
			if(appLogListener != null)
			{
				this.appLogListener.receiveNotify();
			}
		}
        else if(intent.getAction().equals(SipManager.ACTION_SIP_ADDR_UPT)) //接受短消息广播
        {
            String bodyStr = intent.getStringExtra(SipManager.Body_Str);
            Log.d(MsgFunc.Log_Key, "ACTION_SIP_MESSAGE bodyStr = " + bodyStr);
            if(notifyListener != null) {
                this.notifyListener.receiveAddrUptMsg();
            }
        }
		else if(intent.getAction().equals(SipManager.ACTION_SIP_ONLINE)) //接受短消息广播
		{
			String bodyStr = intent.getStringExtra(SipManager.Body_Str);
			Log.d(MsgFunc.Log_Key, "ACTION_SIP_MESSAGE bodyStr = " + bodyStr);
			if(notifyListener != null) {
				this.notifyListener.receiveOnlineMsg();
			}
		}
	}

	public void sendUrgencyMsg()
	{
//		String num = config.getUrgencyNum();
//		String contect = config.getUrgencyContent();
//		
//		SessionInfo session  = new SessionInfo(contect);
//		UserInfo receiver = this.dbOperater.getUserInfoByNum(num);
//		Log.d(MsgFunc.Log_Key, "sendUrgencyMsg num = " + num + " receiver = " + receiver);
//		if(receiver != null)
//		{
//			session.setReceiver(receiver);
//			session.setMsgtype(SessionInfo.Msgtype_Urgency);
//		}
//		
//		try 
//		{
//			operator.sendMessage(num, session);
//		} catch (RemoteException e) 
//		{
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}

	public void clearResource()
	{
		Log.d(MsgFunc.Log_Key, "MsgBroadcast clearResource");
		this.ctx.unregisterReceiver(this);
		this.ctx = null;
		this.msgListener = null;
	}
}
